package com.springboot.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.springboot.dao.AdminMapper;
import com.springboot.entity.Admin;
import com.springboot.utils.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public ResultVO login(String username, String password) {
        Admin admin = new Admin();
        admin.setUsername(username);
        admin.setPassword(password);
        Admin login = adminMapper.selectOne(admin);
        if(login==null){
            return new ResultVO(100,"用户名或者密码错误",null);
        }else{
            return new ResultVO(101,"登录成功",login);

        }
    }
    @Override
    public ResultVO AdminList(String name, int pageNum) {
        PageHelper.startPage(pageNum,5); //设置分页的页面容量
        Example example=new Example(Admin.class);
        Example.Criteria criteria=example.createCriteria();
//模糊查询
        if(!StringUtils.isEmpty(name)){
//进行模糊查询
            criteria.andLike("name",'%'+name+'%');
        }
        List<Admin> list = adminMapper.selectByExample(example);
        PageInfo<Admin> pageInfo=new PageInfo<Admin>(list);
        return new ResultVO(1005,"查询成功",pageInfo);
    }

    @Override
    public ResultVO addAdmin(Admin admin) {
        int i = adminMapper.insertSelective(admin);  //选择性增加
        if (i > 0) {
            return new ResultVO(2000, "增加成功", i);
        } else {
            return new ResultVO(2001, "增加失败", null);
        }
    }

    @Override
    public ResultVO deleteAdmin(long aid) {
        int n = adminMapper.deleteAdmin(aid);
        if(n>0){
            return new ResultVO(3000,"删除成功",n);
        }else{
            return new ResultVO(3001,"删除失败",null);
        }
    }

    @Override
    public ResultVO saveAdmin(Admin admin) {
        int i = adminMapper.updateByPrimaryKeySelective(admin);
        if(i>0){
            return new ResultVO(3000,"修改成功",i);
        }else{
            return new ResultVO(3001,"修改失败",null);
        }
    }
}
